<template>
  <div>
    <el-row style="margin-bottom:15px">
      <el-col :span="12">
      <span class="titleInfo">{{$t('table.say')}}</span>
      </el-col>
      <!-- 顶部-新增试题 -->
      <el-col :span="12" style="display:flex;justify-content:flex-end">
        <slot name="excelExport-btn"/>
      <el-button
      @click="$router.push('/questions/new?routerJump=1')"
        size="small"
        type="success"
        icon="el-icon-edit"
        >{{$t('table.'+'AddTestQuestions')}}</el-button
      >
      </el-col>

    </el-row>
    <!-- form表单：共12个表单域 -->
    <el-form label-width="85px" :model="formData" ref="form" :rules="rules">
      <el-row>
        <!-- 1.学科 -->
        <el-col :span="6">
          <el-form-item :label="$t('route.'+'subjects-list')" prop="subjectID">
            <el-select
              @change="changeSubject"
              v-model="formData.subjectID"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in subjectList"
                :key="item.value"
                >{{ item.label }}</el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 2.二级目录 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'SecondaryDirectory')" prop="catalogID">
            <el-select
              v-model="formData.catalogID"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                :label="item.directoryName"
                :value="item.id"
                v-for="item in catalog"
                :key="item.id"
                >{{ item.directoryName }}</el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 3.标签 -->
        <el-col :span="6">
          <el-form-item :label="$t('route.'+'subjects-tags')" prop="tags">
            <el-select
              v-model="formData.tags"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                :label="item.lebel"
                :value="item.value"
                v-for="item in tagList"
                :key="item.value"
                >{{ item.label }}</el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 4.关键字 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'keyword')" prop="keyword">
            <el-input size="small" v-model="formData.keyword"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 5.试题类型-->
      <el-row>
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'QuestionType')" prop="questionType">
            <el-select
            v-model="formData.questionType"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option :label="item.label" :value="item.value" v-for="item in questionType" :key="item.value">{{item.label}}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 6.难度 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'difficulty')" prop="difficulty">
            <el-select
            v-model="formData.difficulty"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option :label="item.label" :value="item.value" v-for="item in difficulty" :key="item.value">{{item.label}}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 7.方向 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'Directions')" prop="direction">
            <el-select
            v-model="formData.direction"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option :label="item" :value="item" v-for="(item,index) in direction" :key="index">{{item}}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 8.录入人 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'EnteredBy')" prop="creatorID">
            <el-select
            v-model="formData.creatorID"
              size="small"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option :label="item.username" :value="item.id" v-for="item in creatorList" :key="item.id">{{item.username}}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!-- 9.题目备注 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'TopicRemarks')" prop="remarks">
            <el-input size="small" v-model="formData.remarks"></el-input>
          </el-form-item>
        </el-col>
        <!-- 10.企业简称 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'Enterprise')" prop="shortName">
            <el-input size="small" v-model="formData.shortName"></el-input>
          </el-form-item>
        </el-col>
        <!-- 11.城市 -->
        <el-col :span="6">
          <el-form-item :label="$t('table.'+'city')" prop="province">
            <el-row>
                <!-- 省 -->
              <el-col :span="12" style="padding-right: 5px">
                <el-select
                @change="changeProvince"
                 v-model="formData.province"
                  size="small"
                  placeholder="请选择"
                  style="width: 100%"
                >
                <el-option :value="item" :label="item" v-for="(item,index) in provinceList" :key="index">{{item}}</el-option>
                </el-select>
              </el-col>
              <!-- 市 -->
              <el-col :span="12" style="padding-left: 5px">
                <el-select
                  v-model="formData.city"
                  size="small"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option  :value="item" v-for="(item,index) in citys" :key="index">{{item}}</el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <!-- 12.清除、搜索 -->
        <el-col :span="6">
          <el-form-item style="float: right">
            <el-button size="small" @click="resetFormData">{{$t('table.'+'eliminate')}}</el-button>
            <el-button size="small" @click="submitFilter" type="primary">{{$t('table.'+'search')}}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { questionType, difficulty, direction } from '@/api/hmmm/constants' // 枚举

import { list as directorysListApi } from '@/api/hmmm/directorys'
import { simple as subjectSimpleApi } from '@/api/hmmm/subjects'
import { list as userListApi } from '@/api/base/users'
import { simple } from '@/api/hmmm/tags'
import { provinces, citys } from '@/api/hmmm/citys' // 枚举

export default {
  name: 'questionsFilter',
  data () {
    return {
      rules: {
        tags: [], // 标签名称
        keyword: [], // 关键字
        questionType: [], // 试题类型
        difficulty: [], // 难度
        direction: [], // 方向
        creatorID: [], // 录入人
        remarks: [], // 题目备注
        shortName: [], // 企业简称
        province: [], // 企业所在地省份
        city: [] // 企业所在城市
      },
      // 搜索绑定的表单
      formData: {
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: null, // 标签名称
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 企业所在地省份
        city: null // 企业所在城市
      },
      catalog: [], // 二级目录
      tagList: [], // 标签
      subjectList: [], // 学科
      creatorList: [], // 录入人
      questionType: questionType, // 试题类型--枚举
      difficulty: difficulty, // 难度--枚举
      direction: direction, // 方向--枚举
      provinceList: provinces(), // 城市--枚举
      citys: citys() // 县--枚举

    }
  },
  created () {
    this.getsubjectsList() // 学科下拉菜单
    this.userList() // 录入人列表
  },
  methods: {
    // 二级目录列表+标签列表
    async changeSubject (subjectId) {
      // 二级目录列表
      this.catalog = this.tagList = []
      this.formData.catalogID = this.formData.tags = null
      const { data: { items } } = await directorysListApi({ subjectID: subjectId })
      this.catalog = items
      // 标签列表
      const { data } = await simple({ subjectID: subjectId })
      this.tagList = data
    },
    // 学科下拉菜单
    async getsubjectsList () {
      const { data } = await subjectSimpleApi({ page: 1, pagesize: 99999 })
      this.subjectList = data
    },
    // 录入人列表
    async userList () {
      const { data: { list } } = await userListApi()
      this.creatorList = list
    },
    // 城市，动态选择下级县
    changeProvince (city) {
      // 城市变化，清空上次的县
      this.citys = []
      this.formData.city = ''
      // 使用枚举的方法
      this.citys = citys(city)
    },
    // 清空表单
    resetFormData () {
      this.$refs.form.resetFields()
      this.formData.city = ''
      // 重新加载第一页的数据
      this.$emit('setList')
    },
    // 搜索
    submitFilter () {
      this.$emit('submitFilter', this.formData)
    }
  }
}
</script>

<style lang="scss" scoped>
.titleInfo {
  font-size: 12px;
  color: pink;
}
.el-form {
  .el-form-item {
    // box-shadow: 0px 2px 5px rgb(20, 20, 20);
    margin-left: 10px;
  }
}
</style>
